﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.DirectoryServices;
using System.Runtime.InteropServices;

namespace Connect
{
     class Network
    {
        [DllImport("iphlpapi.dll", ExactSpelling = true)]
        public static extern int SendARP(int DestIP, int SrcIP, [Out] byte[] pMacAddr, ref int PhyAddrLen);
        /// <summary>
        /// Hiện danh sách các máy trong mạng LAN
        /// </summary>
        /// <param name="WorkGroup">WorkGroup : Nhóm làm việc</param>
        /// <param name="HideOffline">true : Ẩn các máy Offline / false ; Hiện các máy Offline </param>
        /// <returns></returns>
        public static DataTable DanhSachMay(string WorkGroup,bool HideOffline)
        {
            try
            {
                DataTable tb = new DataTable();
                tb.Columns.Add("Computer");
                tb.Columns.Add("IP");
                tb.Columns.Add("MAC");

                if (string.IsNullOrEmpty(WorkGroup)==true)
                {
                    return null;
                }


                // Use Your work Group WinNT://&&&&(Work Group Name)
                DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + WorkGroup.Trim());
                DomainEntry.Children.SchemaFilter.Add("computer");


                // To Get all the System names And Display with the Ip Address
                foreach (DirectoryEntry machine in DomainEntry.Children)
                {
                    DataRow dr = tb.NewRow();
                    dr[0] = machine.Name;
                    System.Net.IPHostEntry Tempaddr = null;

                    try
                    {
                        Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);

                    }
                    catch (Exception)
                    {
                        if (HideOffline==false)
                        {
                        dr[0] = machine.Name;
                        dr[1] = "Offline";
                        tb.Rows.Add(dr);                         
                        }

                        continue;
                    }
                    System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
                    foreach (IPAddress TempA in TempAd)
                    {
                        dr[1] = TempA.ToString();
                        byte[] ab = new byte[6];
                        int len = ab.Length;

                        // This Function Used to Get The Physical Address
                        int r = SendARP((int)TempA.Address, 0, ab, ref len);
                        string mac = BitConverter.ToString(ab, 0, 6);

                        dr[2] = mac;
                    }
                    tb.Rows.Add(dr);
                }
               return tb;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
